package model.elements;

import model.collisions.CollisionCategory;
import model.elements.attributes.Coordinate2D;
import model.elements.attributes.Damage;
import model.elements.attributes.Lives;
import model.elements.behaviours.TakesDamage;
import model.elements.commands.ElementCommandFactory;
import model.elements.states.life.Alive;
import model.elements.states.life.Dead;
import model.elements.states.life.ElementLifeState;
import model.exceptions.CommandFactoryIsNotReady;
import model.exceptions.ModelExceptions;

// TODO: Auto-generated Javadoc
/**
 * The Class Wall.
 */
public class Wall extends Element2D implements TakesDamage {
	
	/** The m_lives. */
	private Lives 					m_lives;			// Vidas que bajan con daño
	
	/** The m_life state. */
	private ElementLifeState		m_lifeState; 
	
	/**
	 * Inits the.
	 *
	 * @param category the category
	 * @param lives the lives
	 * @throws CommandFactoryIsNotReady the command factory is not ready
	 */
	private void init(CollisionCategory category, int lives) throws CommandFactoryIsNotReady {
		m_lifeState = new Alive( ElementCommandFactory.getElementDoesNothing() );
		setCollisionCategory( category );
		m_lives 		= new Lives(lives);
	}
	
	/**
	 * Instantiates a new wall.
	 *
	 * @param position the position
	 * @throws ModelExceptions the model exceptions
	 */
	public Wall(Coordinate2D position) throws ModelExceptions {
		super(position);
		init(CollisionCategory.C_T1_WALL, 5);
	}
	
	/**
	 * Instantiates a new wall.
	 *
	 * @param position the position
	 * @param category the category
	 * @param lives the lives
	 * @throws ModelExceptions the model exceptions
	 */
	public Wall(Coordinate2D position, CollisionCategory category, int lives) throws ModelExceptions {
		super(position);
		init(category, lives);
	}

	/* (non-Javadoc)
	 * @see model.elements.Element2D#getPosition()
	 */
	@Override
	public Coordinate2D getPosition() {
		return m_currentPosition;
	}

	/* (non-Javadoc)
	 * @see model.elements.Element2D#getCollisionCategory()
	 */
	@Override
	public CollisionCategory getCollisionCategory() {
		return m_lifeState.getCategory();
	}

	
	/**
	 * Sets the collision category.
	 *
	 * @param category the new collision category
	 */
	protected void setCollisionCategory(CollisionCategory category) {
		m_lifeState.setCategory(category);
	}

	/**
	 * * TakesDamage Interface.
	 *
	 * @param damage the damage
	 * @throws CommandFactoryIsNotReady **
	 */
	
	@Override
	public void takeDamage( Damage damage ) throws CommandFactoryIsNotReady
	{
		m_lives.setValue( m_lives.getValue() - damage.getDamageValue() );
		
		if ( m_lives.getValue() <= 0 )
			m_lifeState = new Dead();
		
	}

	/* (non-Javadoc)
	 * @see model.elements.Element2D#isAlive()
	 */
	@Override
	public boolean isAlive() {
		return m_lifeState.isAlive();
	}


	/* (non-Javadoc)
	 * @see model.elements.states.life.Mortal#onDeath()
	 */
	@Override
	public void onDeath() {}

}
